package com.yanqu.road.server.channelserver.cmd.xrank.season;

import com.yanqu.road.entity.enums.rank.eRankRangeType;
import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankListModel;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankListModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.xrank.ChannelXRankMgr;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = ChannelProtocol.N_XRANK_REQ_SYNC_RANK_LIST, desc = "")
public class XRankReqSyncRankListCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.ChannelSyncXRankListReqMsg reqMsg = XRankProto.ChannelSyncXRankListReqMsg.parseFrom(packet.getBytes());
        int range = reqMsg.getRange();

        if (range == eRankRangeType.User.getValue()) {
            XUserRankListModel rankListModel = ChannelXRankMgr.getUserRankMap().getUserRankListModel(reqMsg.getActivityId(), reqMsg.getConditionType());
            XRankProto.XUserRankListMsg.Builder builder = XRankPb.parseXUserRankListMsg(reqMsg.getActivityId(), reqMsg.getConditionType(), rankListModel.getRankList(), false);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_XRANK_SYNC_USER_RANK_FROM_CHANNEL, builder);
            ChannelMessageHelper.sendToChannel(message, reqMsg.getChannelId());

        } else if (range == eRankRangeType.Channel.getValue()) {
            XChannelRankListModel rankListModel = ChannelXRankMgr.getChannelRankMap().getChannelRankListModel(reqMsg.getActivityId(), reqMsg.getConditionType());
            for (int channelId : rankListModel.getPartMap().keySet()) {
                XRankProto.XChannelRankPartListMsg.Builder builder = XRankPb.parseXChannelRankPartListMsg(reqMsg.getActivityId(), reqMsg.getConditionType(), rankListModel.getPartValueRankList(channelId));
                YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_XRANK_SYNC_CHANNEL_RANK_PART_FROM_CHANNEL, builder);
                ChannelMessageHelper.sendToChannel(pbMessage, channelId);
            }

        } else {
            log.info("can not find range {} activityId {} type {}.", range, reqMsg.getActivityId(), reqMsg.getConditionType());
        }

        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
    }

    @Override
    public void sendData() {
    }
}
